#include "Texture.h"

Texture::Texture(int width, int height, int bpp, void *data)
{
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);

	GLenum format = 0;
	if (bpp == 24)
		format = GL_RGB;
	if (bpp == 32)
		format = GL_RGBA;

	if (data != NULL)
		gluBuild2DMipmaps(GL_TEXTURE_2D, bpp / 8, width, height, format, GL_UNSIGNED_BYTE, data);
	else
		glTexImage2D(GL_TEXTURE_2D, 0, bpp / 8, width, height, 0, format, GL_UNSIGNED_BYTE, data);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glBindTexture(GL_TEXTURE_2D, 0);
}

Texture::~Texture()
{
	if (id != NULL)
		glDeleteTextures(1, &id);
}

void Texture::BindTexture()
{
	glBindTexture(GL_TEXTURE_2D, id);
}

UINT Texture::GetId()
{
	return id;
}